public class LinkedList {

    private Node head;
    private int count;


    public LinkedList() {
        head = new Node();
    }

    /**
     * 更新数组指定下标的元素
     *
     * @param index   待更新的元素下标
     * @param element 更新后的元素对象
     * @return 被替换掉的旧对象
     */
    public Object set(int index, Object element) {
        if (index < 0 || index > count) {
            System.out.println("index不合法!");
            return null;
        }
        Node temp = head;
        for (int i = 0; i <= index; i++) {
            temp = temp.getNext();
        }
        Object rlt = temp.getValue();

        temp.setValue(element);

        return rlt;
    }

    /**
     * 查询指定位置下标的元素
     *
     * @param index 需要判断index是否合法
     * @return 返回查找到的元素，找不到返回null
     */
    public Object get(int index) {
        if (index < 0 || index > count) {
            System.out.println("index不合法!");
            return null;
        }
        Node temp = head;
        for (int i = 0; i <= index; i++) {
            temp = temp.getNext();
        }
        return temp.getValue();
    }

    /**
     * 清空集合内容
     */
    public void clear() {
        Node temp = head;
        head.setNext(null);
        head.setValue(0);
//        for (int i = 0; i <= count; i++) {
//            temp = temp.getNext();
//            temp.setPrev(null);
//            temp.setNext(null);
//            temp.setValue(null);
//        }
//        temp.setNext(null);
        count = 0;
    }

    /**
     * 删除指定下标的元素
     *
     * @param index 元素下标
     * @return 将删除的元素返回，如果下标不合理，返回null
     */
    public Object remove(int index) {
        if (index < 0 || index > count) {
            System.out.println("index不合法!");
            return null;
        }
        Node temp = head;
        for (int i = 0; i <= index; i++) {
            temp = temp.getNext();
        }
        Object rlt = temp.getValue();
        if (temp.getNext() == null){
            temp.getPrev().setNext(null);
        }else {
            temp.getNext().setPrev(temp.getPrev());
            temp.getPrev().setNext(temp.getNext());
            temp.setNext(null);
            temp.setPrev(null);
        }
        count--;

        return rlt;
    }

    /**
     * 查找并删除元素
     *
     * @param o 通过目标元素 equals 方法判断是否匹配，
     *          需要判断o是否为null，如果传入null，则用== 进行比较
     * @return
     */
    public boolean remove(Object o) {
        Node temp = head;
        for (int i = 0; i <= count; i++) {
            temp = temp.getNext();
            if (o == null) {
                if (temp.getValue() == null) {
                    remove(i);
                    return true;
                }
            } else {
                if (temp.getValue() == o) {
                    remove(i);
                    return true;
                }
            }
        }
        return false;
    }


    /**
     * 添加一个元素
     *
     * @param o
     * @return 添加成功后返回true 失败返回false
     */
    public boolean add(Object o) {
        Node newNode = new Node();
        newNode.setValue(o);
        Node temp = head;
        for (int i = 1; i <= count; i++) {
            temp = temp.getNext();
        }
        temp.setNext(newNode);
        newNode.setPrev(temp);
        newNode.setNext(null);
        count++;
        head.setValue(count);
        return true;
    }

    /**
     * @return 数组中没有元素，返回true
     */
    public boolean isEmpty() {
        return count == 0;
    }

    /**
     * 返回数组中保存的元素个数
     *
     * @return
     */
    public int size() {
        return count;
    }

    public static void main(String[] args) {

        LinkedList linkedList = new LinkedList();
        linkedList.add(1);
        linkedList.add(2);
        linkedList.add(3);
        linkedList.add(4);
        linkedList.add(5);

        for (int i = 0; i < linkedList.size(); i++) {
            System.out.println(linkedList.get(i));
        }

        System.out.println(linkedList.isEmpty());

        System.out.println(linkedList.remove(1));

        System.out.println(linkedList.remove(Integer.valueOf(4)));

        System.out.println("============");

        for (int i = 0; i < linkedList.size(); i++) {
            System.out.println(linkedList.get(i));
        }
        linkedList.set(0, 200);
        System.out.println("+++++++++++++");
        for (int i = 0; i < linkedList.size(); i++) {
            System.out.println(linkedList.get(i));
        }

        linkedList.clear();
        System.out.println(linkedList.isEmpty());

    }
}


